package com.pineapple.module.core.planting.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pineapple.module.core.planting.domain.Planting;
import com.pineapple.module.core.planting.domain.PlantingCriteria;
import com.pineapple.module.core.planting.domain.PlantingSearch;

public class PlantingManager {
	PlantingDAO dao = null;

	public PlantingManager(){
		dao = new PlantingDAO();
	}

	public List<PlantingSearch> search(PlantingCriteria criteria,Connection conn)throws Exception{
		List<PlantingSearch> lstPlanting = new ArrayList<PlantingSearch>();
		int totalRacord =dao.count(criteria, conn);
		criteria.setTotalResult(totalRacord);

		if (criteria.getTotalResult() < criteria.getStart()) {
			criteria.setStart(criteria.getStart() - criteria.getLinePerPage());
		}
			if(totalRacord>0){
				lstPlanting = dao.search(criteria, conn);
		}
			return lstPlanting;
	}

	public Map<String,String> pineappleCombo(Connection conn)throws Exception{
		return dao.pineappleCombo(conn);
	}
	public Map<String,String> areaCombo(Connection conn)throws Exception{
		return dao.areaCombo(conn);
	}



	public void add(Connection conn,Planting plating)throws Exception{
		try{
			conn.setAutoCommit(false);
			dao.checkDup(conn,Integer.parseInt(plating.getAreaId()));
			dao.add(conn, plating);
			//dao.update(conn, plating);
			conn.commit();
		}catch (Exception e) {
			conn.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			conn.setAutoCommit(false);
		}
	}
	public void updateStatus(Connection conn,String plantingId,String status) throws Exception{
		dao.updateStatus(conn, plantingId, status);
	}
	public void edit(Connection conn,Planting planting)throws Exception{
		dao.edit(conn, planting);
	}
	public void delete(Connection conn,String plantingIds)throws Exception{
		dao.delete(conn, plantingIds);
	}
	public Planting searchById(Connection conn,int id)throws Exception{
		return dao.searchById(conn, id);
	}

}
